package com.hzt.business.controller;

import com.hzt.business.domain.Goods;
import com.hzt.business.service.GoodsService;
import com.hzt.business.vo.GoodsVo;
import com.hzt.system.common.Constant;
import com.hzt.system.common.DataGridView;
import com.hzt.system.common.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
* 
* 
* @Author:hzt
* @Date:6:52 下午 2020/7/25
*/
@RequestMapping("api/goods")
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("loadAllGoods")
    public Object loadAllGoods(GoodsVo goodsVo){
        return this.goodsService.queryAllGoods(goodsVo);
    }


    @RequestMapping("addGoods")
    public ResultObj addGoods(Goods goods){

        try {
            goods.setAvailable(Constant.AVAILABLE_TRUE);
            goodsService.saveGoods(goods);
            return  ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return  ResultObj.ADD_SUCCESS;
        }
    }


    @RequestMapping("updateGoods")
    public ResultObj updateGoods(Goods goods){
        try {
            goodsService.updateGoods(goods);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }


    @RequestMapping("deleteGoods")
    public ResultObj deleteGoods(Goods goods){
        try {
            goodsService.removeById(goods.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    //批量删除
    @RequestMapping("batchDeleteGoods")
    public ResultObj batchDeleteGoods(Integer[] ids){
        try {
            List<Integer> listIds=new ArrayList<>();
            for (Integer id : ids) {
                listIds.add(id);
            }
            goodsService.removeByIds(listIds);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


    @RequestMapping("getAllAvailableGoods")
    public DataGridView getAllAvailableGoods(){
        return this.goodsService.getAllAvailableGoods();
    }



    /**
     * 根据供应商id查询商品id
     */
    @RequestMapping("getGoodsByProviderid")
    public DataGridView getGoodsByProviderid(Integer providerid){
    return  this.goodsService.getGoodsByProviderid(providerid);
    }


    /**
     * 根据商品id查询商品信息
     */
    @RequestMapping("getGoodsByGoodId")
    public DataGridView getGoodsByGoodId(Integer goodsid){
        return  new DataGridView(this.goodsService.getById(goodsid));
    }

}
